import numpy as np
from PIL import Image

n_bin = 10

def encode_color(img:Image):
    np_img = np.array(img)
    if len(np_img.shape) == 2:
        # 灰度图像，复制三个分量
        np_img = np.repeat(np_img[:,:,np.newaxis], 3, axis=2)
    rh = np.histogram(np_img[:,:,0], bins=n_bin)
    red_features = (rh[0] / rh[0].sum()).astype(np.float32)
    
    gh = np.histogram(np_img[:,:,0], bins=n_bin)
    green_features = (gh[0] / gh[0].sum()).astype(np.float32)
    
    bh = np.histogram(np_img[:,:,0], bins=n_bin)
    blue_features = (bh[0] / bh[0].sum()).astype(np.float32)

    return np.concatenate([red_features, green_features, blue_features])